package com.zx.service.impl.dj;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.dao.dj.RoleRecordMapper;
import com.zx.model.dj.PriceRecord;
import com.zx.model.dj.RoleRecord;
import com.zx.model.user.User;
import com.zx.service.dj.RoleManageService;
import com.zx.utils.DateUtils;

/**
 * 
 * @author yt
 *
 */
@Service
public class RoleManageServiceImpl implements RoleManageService {

    @Autowired
    private RoleRecordMapper roleRecordMapper;

    /**
     * 插入角色
     */
	@Override
	public void insRole(String name, String password, String role, String function) {
        RoleRecord rr = new RoleRecord();
        rr.setName(name);
        rr.setPassword(password);       
        rr.setRole(role);
        rr.setCreateTime(DateUtils.getCurrentTime());
        rr.setUpdateTime(DateUtils.getCurrentTime());

        if("1".equals(function)){
        	//插入角色
        	roleRecordMapper.insert(rr);
        	
        }else{
        	//删除角色
        	roleRecordMapper.delete(rr);
        }
		
	}

	@Override
	public int uptPassword(String name, String password, String newpassword, String againpassword) {
		String a = null;
        List<RoleRecord> list1 = roleRecordMapper.select(name);
		for (int i = 0; i < list1.size(); i++) {
			RoleRecord rr=list1.get(i);
			a = rr.getPassword();
			}
		if(a != null && newpassword != null && againpassword != null){
			if(!(a.equals(newpassword)) && newpassword.equals(againpassword)){
		        RoleRecord rrd = new RoleRecord();
		        rrd.setName(name);
		        rrd.setPassword(newpassword);       
		        rrd.setUpdateTime(DateUtils.getCurrentTime());
				roleRecordMapper.update(rrd);
				return 0;
			}else{
				return 1;
			}
			}else{
				return 2;
			}
        
		}

	@Override
	public User selRole(String name) {
		User u = new User();
		RoleRecord rr = roleRecordMapper.selectByName(name);
		u.setUserName(rr.getName());
		u.setPassword(rr.getPassword());
		return u;
		
	}
}